#!/usr/bin/php
<?php

/**
* FeedGator - Aggregate RSS newsfeed content into a Joomla! database
* @version 2.3.3
* @package FeedGator
* @author Original author Stephen Simmons
* @now continued and modified by Matt Faulds, Remco Boom & Stephane Koenig and others
* @email mattfaulds@gmail.com
* @Joomla 1.5 Version by J. Kapusciarz (mrjozo)
* @copyright (C) 2005 by Stephen Simmons - All rights reserved
* @license GNU/GPL: http://www.gnu.org/copyleft/gpl.html
*
**/


define( '_JEXEC', 1 );

define('DS', DIRECTORY_SEPARATOR);
define('JPATH_BASE', substr(__FILE__,0,strrpos(__FILE__, DS.'administrator')));

require_once(JPATH_BASE.DS.'includes'.DS.'defines.php' );
require_once(JPATH_ADMINISTRATOR.DS.'includes'.DS.'framework.php' );
require_once(JPATH_ADMINISTRATOR.DS.'includes'.DS.'helper.php' );
require_once(JPATH_ADMINISTRATOR.DS.'includes'.DS.'toolbar.php' );

$mainframe = &JFactory::getApplication('site');
JRequest::setVar('cron',1);
$mainframe->initialise();


$version = new JVersion();
define('J_VERSION', $version->getShortVersion());

require_once(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_feedgator'.DS.'controller.php');
require_once(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_feedgator'.DS.'factory.feedgator.php');
require_once(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_feedgator'.DS.'helpers'.DS.'feedgator.helper.php');
require_once(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_feedgator'.DS.'helpers'.DS.'feedgator.utility.php');
if(file_exists(JPATH_ROOT.DS.'plugins'.DS.'system'.DS.'addkeywords.php')) {
	require_once(JPATH_ROOT.DS.'plugins'.DS.'system'.DS.'addkeywords.php');
}
FeedgatorUtility::profiling('Start cron');

define('SPIE_CACHE_AGE', 60*10);

require_once(JPATH_ROOT.DS.'libraries'.DS.'simplepie'.DS.'simplepie.php');
require_once(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_feedgator'.DS.'inc'.DS.'simplepie'.DS.'overrides.php');
FeedgatorUtility::profiling('Loaded SimplePie');

JRequest::setVar('task','cron','get');
JRequest::setVar(JUtility::getToken(),'1','get');

$config = array('base_path'=>JPATH_ADMINISTRATOR.DS.'components'.DS.'com_feedgator');
$controller = new FeedgatorController($config);
$controller->import('all');

FeedgatorUtility::profiling('End');
// processing duplicates
$toolsModel = &FGFactory::getToolsModel();
if($dups = $toolsModel->getDuplicates()) {	
	$db		= JFactory::getDbo();
	foreach($dups as &$dup) {
		//$plugin = $model->getPlugin($dup->content_type);
		$data = explode('||',$dup->results);
		foreach($data as &$datum) {
			$datum = explode('|',$datum);
		}
		$dup->dups = array();
		for($i=1;$i<$dup->num;$i++) {
			$d = new StdClass();
			$d->id = @$data[$i][0];
			//$d->content_link = $plugin->getContentLink($d->id);
			$d->sectionid = @$data[$i][1];
			$d->catid = @$data[$i][2];
			$d->title = @$data[$i][3];
			$db->setQuery("UPDATE #__content set state ='-2', ordering='0' where id ='".$d->id."'");
			$db->query();
		}
	}
}	
echo 'Import finished';